*******************************************************************************
* Open data
*******************************************************************************

use dataset_polls, clear

*******************************************************************************
* Recode party
*******************************************************************************

gen party = .
replace party = 1 if regex(lower(subsample), "dem")
replace party = 0 if regex(lower(subsample), "rep")
keep if !mi(party)

*******************************************************************************
* If both vote choice and party included, take the average so as not to inflate
* sample
*******************************************************************************

collapse cycle legitimatec, by(house date party)

*******************************************************************************
* Set cutoffs for post-election periods
*******************************************************************************

local c2000b = date("12-12-2000", "MDY")
local c2000c = date("11-02-2004", "MDY")
local c2016 = date("11-08-2016", "MDY")
local c2020 = date("11-03-2020", "MDY")

gen Z = .
replace Z = 0 if date > `c2000b' & date < `c2000c' & cycle == 2000
replace Z = 1 if date > `c2016' & date < `c2020' & cycle == 2016
replace Z = 2 if date > `c2020' & cycle == 2020
tab Z

*******************************************************************************
* Poll fixed effects
*******************************************************************************

egen house_date = group(house date)

*******************************************************************************
* Dummy for losers
*******************************************************************************

gen loser = .
replace loser = 1 if party == 1 & inlist(Z, 0, 1)
replace loser = 1 if party == 0 & inlist(Z, 2)
replace loser = 0 if party == 1 & inlist(Z, 2)
replace loser = 0 if party == 0 & inlist(Z, 0, 1)

*******************************************************************************
* More dummies for the regression
*******************************************************************************

tabulate Z, gen(Z_)
drop Z_1
gen Z_2016_loser = Z_2*loser
gen Z_2020_loser = Z_3*loser
rename Z_2 Z_2016
rename Z_3 Z_2020

*******************************************************************************
* Do the regressions
*******************************************************************************

eststo clear
eststo: reg legitimatec loser Z_*, cl(house_date) robust
eststo: areg legitimatec loser Z_*, cl(house_date) a(house) robust
estadd local housefe = "X"
eststo: areg legitimatec loser Z_* date, cl(house_date) a(house) robust
estadd local housefe = "X"
estadd local linear = "X"
eststo: areg legitimatec loser Z_* c.date##c.date, cl(house_date) ///
	a(house) robust
estadd local housefe = "X"
estadd local quadratic = "X"
esttab, compress se

*******************************************************************************
* Output
*******************************************************************************

label variable Z_2016 "2016 dummy"
label variable Z_2020 "2020 dummy"
label variable loser "Voted for loser"
label variable Z_2016_loser "2016 X loser"
label variable Z_2020_loser "2020 X loser"

#delimit;

esttab
	using "_output/table_regression.tex"
	,
		replace
		booktabs
		label
		nomtitles
		collabels(none)
		cells(b(fmt(3) star) se(par))
		order(loser Z_2016_loser Z_2020_loser)
		width(\linewidth)
		drop(*date*)
		stats(housefe linear quadratic N, 
			label(
				"House Fixed Effects"
				"Linear Trends"
				"Quadratic Trends"
				"Sample Size"
			)
			fmt(%20.0f %20.0f))
		compress
		gap
		alignment(l)
		;
			
#delimit cr	


*******************************************************************************
* End
*******************************************************************************
